package scheduleFile;


import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

import schedule.Event;
import schedule.Schedule;

/**
 * Permet de charger et de sauvegarder un emploi du temps dans le format XML
 * @author Stanislas KOBAK / Celine BARILLERE
 *
 */
public class CdmFile
{
	public Schedule load(InputStream inputStream) throws ParserConfigurationException, SAXException, IOException
	{		
		SAXParserFactory factory = SAXParserFactory.newInstance();
		SAXParser parser = factory.newSAXParser();
		ScheduleHandler handler = new ScheduleHandler();
		parser.parse(inputStream, handler);
		return handler.getSchedule();
	}

	public void save(Schedule schedule, FileOutputStream outputStream) throws ParserConfigurationException, TransformerException
	{
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder;
		builder = factory.newDocumentBuilder();
		Document document = builder.newDocument();
		document.setXmlVersion("1.0");

		Element root = document.createElement("schedule");
		document.appendChild(root);

		Element name = document.createElement("name");
		name.setTextContent(schedule.getName());
		root.appendChild(name);

		Element events = document.createElement("events");
		root.appendChild(events);
		
		for(Event event : schedule)
		{
			Element eventElement = document.createElement("event");
			events.appendChild(eventElement);
			
			Element subject = document.createElement("subject");
			subject.setTextContent(event.getSubject());
			eventElement.appendChild(subject);
			
			Element place = document.createElement("place");
			place.setTextContent(event.getPlace());
			eventElement.appendChild(place);
			
			Element date = document.createElement("date");
			date.setTextContent(event.getStartDate().toString());
			eventElement.appendChild(date);
			
			Element duration = document.createElement("duration");
			duration.setTextContent(String.valueOf(event.getDuration()));
			eventElement.appendChild(duration);
			
			Element persons = document.createElement("persons");
			eventElement.appendChild(persons);
			
			for(String person : event.getPersons())
			{
				Element personElement = document.createElement("person");
				subject.setTextContent(person);
				persons.appendChild(personElement);
			}
		}
		
		Source source = new DOMSource(document);
		Result resultat = new StreamResult(outputStream);
		
		// Configuration du transformer
		TransformerFactory fabrique = TransformerFactory.newInstance();
		Transformer transformer = fabrique.newTransformer();
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");
		transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
		
		// Transformation
		transformer.transform(source, resultat);
	}
}
